//
//  TrainingDetailViewController.m
//  B
//
//  Created by lanou on 16/5/17.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "TrainingDetailViewController.h"
#import "TrainingDetailTableViewCell.h"
#import <AVFoundation/AVFoundation.h>

@interface TrainingDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AVPlayerLayer *layer;
@property (nonatomic, strong) UIView *aView;
@property (nonatomic, strong) UIButton *button;
@end

@implementation TrainingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 + 116)];
    [self.view addSubview:_aView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = self.training.title;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3 + 50, self.view.frame.size.width, self.view.frame.size.height * 2 / 3) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //_tableView.backgroundColor = [UIColor redColor];
    [_tableView registerNib:[UINib nibWithNibName:@"TrainingDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.bounces = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

- (void)finish
{
    self.button = nil;
    self.button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _button.frame = CGRectMake((self.aView.frame.size.width - 60) / 2, (self.aView.frame.size.height - 60) / 2, 60, 60);
    [_button setTitle:@"播放" forState:(UIControlStateNormal)];
    [_button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [_button setImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
    [_button addTarget:self action:@selector(playVideo) forControlEvents:(UIControlEventTouchUpInside)];
    [self.aView addSubview:_button];
}

- (void)playVideo
{
    [self play];
}

- (void)viewWillAppear:(BOOL)animated
{
    if (_layer) {
        [self.layer removeFromSuperlayer];
    }
    [self play];
}

- (void)play
{
    
    self.player = [AVPlayer playerWithURL:[NSURL URLWithString:self.training.video]];
    self.layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    
    _layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 + 116);
    [self.aView.layer addSublayer:_layer];
    
    [_player play];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height * 2 / 3 + 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrainingDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLable.text = self.training.title;
    cell.trainingBodyPartsLable.text = self.training.trainingBodyParts;
    cell.postureLable.text = self.training.posture;
    cell.actionDescpLable.text = self.training.actionDescp;
    cell.actionPivotalLable.text = self.training.actionPivotal;
    cell.memoLable.text = self.training.memo;
    cell.backgroundColor = [UIColor colorWithRed:0.889 green:0.906 blue:1.000 alpha:1.000];
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
